package com.pinyougou.shop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.pojo.Goods;
import com.pinyougou.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Reference(timeout = 10000)
    private GoodsService goodsService;

    @Autowired
    private MQProducer mqProducer;


    //添加商品
    @PostMapping("/save")
    public boolean save(@RequestBody Goods goods){
        try {
            //获取登录用户名
            String  sellerId = (String) SecurityUtils.getSubject().getPrincipal();
            //设置商家ID
            goods.setSellerId(sellerId);
            goodsService.save(goods);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
    @GetMapping("/findByPage")
    public PageResult findByPage(Goods goods,Integer page,@RequestParam(defaultValue = "10")Integer rows){
        //获取登录商家编号
        String sellerId = (String) SecurityUtils.getSubject().getPrincipal();
        //添加查询条件
        goods.setSellerId(sellerId);
        /** GET请求中文转码 */
        if (StringUtils.isNoneBlank(goods.getGoodsName())) {
            try {
                goods.setGoodsName(new String(goods
                        .getGoodsName().getBytes("ISO8859-1"), "UTF-8"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return goodsService.findByPage(goods,page,rows);
    }

    @GetMapping("/updateMarketable")
    public boolean updateMarketable(Long[]ids,String status){

        try {
            // 修改数据库表可销售状态
            goodsService.updateStatus("is_marketable", ids, status);

            // 判断商品上下架状态
            if ("1".equals(status)){ // 表示商品上架
                // 发送消息，生成商品索引
                mqProducer.send(new Message("ES_ITEM_TOPIC", "UPDATE",
                        JSON.toJSONString(ids).getBytes("UTF-8")));

                // 发送消息到消息中间件（生成商品的静态页面）
                mqProducer.send(new Message("PAGE_ITEM_TOPIC", "CREATE",
                        JSON.toJSONString(ids).getBytes("UTF-8")));

            }else { // 表示商品下架
                // 发送消息，删除商品索引
                mqProducer.send(new Message("ES_ITEM_TOPIC", "DELETE",
                        JSON.toJSONString(ids).getBytes("UTF-8")));
                // 发送消息到消息中间件（删除商品的静态页面）
                mqProducer.send(new Message("PAGE_ITEM_TOPIC", "DELETE",
                        JSON.toJSONString(ids).getBytes("UTF-8")));
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}
